#include "cpurankviewmodel.h"
#include "qdebug.h"

CpuRankInfo::CpuRankInfo(const QVariantMap &map) {
    id = map.value("id").toString();
    pai_ming = map.value("pai_ming").toInt();
    ming_cheng = map.value("ming_cheng").toString();
    shu_zhi = map.value("shu_zhi").toString();
    bai_fen_bi = map.value("bai_fen_bi").toDouble();

    this->data = map;

    ext_switched = false;
}

CpuRankViewModel::CpuRankViewModel(QObject *parent)
    : QAbstractListModel{parent}
{

}

void CpuRankViewModel::append(CpuRankInfo *newCpuRankInfo)
{
    qDebug() << "CpuRankViewModel:" << newCpuRankInfo->id;
    m_cpuRankInfos.append(newCpuRankInfo);

    emit layoutChanged();
}

void CpuRankViewModel::setFilter(const QString &cpu_ming_cheng)
{

}

QList<CpuRankInfo *> CpuRankViewModel::cpuRankInfos() const
{
    return m_cpuRankInfos;
}

void CpuRankViewModel::setCpuRankInfos(const QList<CpuRankInfo *> &newCpuRankInfos)
{
    if (m_cpuRankInfos == newCpuRankInfos)
        return;
    m_cpuRankInfos = newCpuRankInfos;
    emit cpuRankInfosChanged();
}

QVariant CpuRankViewModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        auto info = m_cpuRankInfos.at(index.row());
        switch ((CpuRankRole)role) {
        case CpuRankId:
            return info->id;
        case CpuRankPaiMing:
            return info->pai_ming;
        case CpuRankMingCheng:
            return info->ming_cheng;
        case CpuRankShuZhi:
            return info->shu_zhi;
        case CpuRankBaiFenBi:
            return info->bai_fen_bi;
        case CpuRankData:
            return info->data;

        case CpuRankExtSwitched:
            return info->ext_switched;
        }
    }
    return QVariant();
}

bool CpuRankViewModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid()) {
        auto info = m_cpuRankInfos.at(index.row());
        switch ((CpuRankRole)role) {
        case CpuRankExtSwitched:
            info->ext_switched = value.toBool();
            return true;
        default:
            break;
        }
    }
    return false;
}

int CpuRankViewModel::rowCount(const QModelIndex &parent) const
{
    return m_cpuRankInfos.count();
}
